<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="200px">

      <!-- 主菜单 -->
      <el-menu
        v-if="!showProfileSubMenu"
        active-text-color="#ffd04b"
        background-color="#232323"
        :default-active="activeMenu"
        text-color="#fff"
        router
      >
        <!-- 主菜单标题 -->
        <el-menu-item index="/admin">
          <el-icon><Menu /></el-icon>
          <span>主菜单</span>
        </el-menu-item>

        <!-- 用户管理 -->
        <el-menu-item index="/admin/user-management">
          <el-icon><UserFilled /></el-icon>
          <span>用户管理</span>
        </el-menu-item>

        <!-- 部门管理 -->
        <el-menu-item index="/admin/department-management">
          <el-icon><Department /></el-icon>
          <span>部门管理</span>
        </el-menu-item>

        <!-- 系统设置 -->
        <el-menu-item index="/admin/system-settings">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </el-menu-item>

        <!-- 数据导出 -->
        <el-menu-item index="/admin/data-export">
          <el-icon><Download /></el-icon>
          <span>数据导出</span>
        </el-menu-item>

        <!-- 通知与提醒 -->
        <el-menu-item index="/admin/notification">
          <el-icon><Bell /></el-icon>
          <span>通知与提醒</span>
        </el-menu-item>

        <!-- 个人中心入口 -->
        <el-menu-item @click="showProfileSubMenu = true">
          <el-icon><User /></el-icon>
          <span>个人中心</span>
        </el-menu-item>
      </el-menu>

      <!-- 个人中心子菜单 -->
      <el-menu
        v-else
        active-text-color="#ffd04b"
        background-color="#232323"
        text-color="#fff"
        :default-active="activeMenu"
        router
      >
        <!-- 返回按钮 -->
        <el-menu-item @click="showProfileSubMenu = false">
          <el-icon><ArrowLeft /></el-icon>
          <span>返回主菜单</span>
        </el-menu-item>

        <!-- 个人中心子菜单项 -->
        <el-menu-item index="/admin/profile">
          <el-icon><User /></el-icon>
          <span>基本资料</span>
        </el-menu-item>
        <el-menu-item index="/admin/avatar">
          <el-icon><Crop /></el-icon>
          <span>更换头像</span>
        </el-menu-item>
        <el-menu-item index="/admin/repassword">
          <el-icon><EditPen /></el-icon>
          <span>重置密码</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 右侧内容区域 -->
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div></div>
        <el-dropdown placement="bottom-end" @command="handleCommand">
          <span class="el-dropdown__box">
            <el-avatar :src="avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="/admin/profile" :icon="User">基本资料</el-dropdown-item>
              <el-dropdown-item command="/admin/avatar" :icon="Crop">更换头像</el-dropdown-item>
              <el-dropdown-item command="/admin/repassword" :icon="EditPen">重置密码</el-dropdown-item>
              <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <!-- 主内容区域 -->
      <el-main>
        <router-view @menu-change="handleMenuChange"></router-view>
      </el-main>

      <!-- 底部 -->
      <el-footer>Created by Goalance</el-footer>
    </el-container>
  </el-container>
</template>

<script setup>
// 导入图标组件
import { OfficeBuilding as Department } from '@element-plus/icons-vue'
import { ref, computed, getCurrentInstance } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import {
  Menu,
  UserFilled,
  Setting,
  Download,
  Bell,
  User,
  Crop,
  EditPen,
  ArrowLeft,
  CaretBottom,
  SwitchButton
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const useStore = useUserStore()
// 获取全局挂载的 $apiBaseUrl
const { proxy } = getCurrentInstance();
const avatar = computed(() => {
  return useStore.user.avatar
    ? proxy.$apiBaseUrl + '/' + useStore.user.avatar
    : '' // 或者默认头像
})
const showProfileSubMenu = ref(false)

// 自动根据路由高亮菜单
const activeMenu = computed(() => route.path)

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('token')
    localStorage.removeItem('role')
    localStorage.removeItem('user')
    router.push('/login')
  } else {
    router.push(command)
    // 如果跳转到个人中心相关页面，显示子菜单
    if (command.includes('/admin/profile') ||
        command.includes('/admin/avatar') ||
        command.includes('/admin/repassword')) {
      showProfileSubMenu.value = true
    } else {
      showProfileSubMenu.value = false
    }
  }
}

// 处理子组件发来的菜单更新请求
const handleMenuChange = (path) => {
  router.push(path)
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #232323;
    position: relative;
    .el-menu {
      border-right: none;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      transition: all 0.3s ease;
    }
  }
  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dropdown__box {
      display: flex;
      align-items: center;
      .el-icon {
        color: #999;
        margin-left: 10px;
      }
      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}

/* 返回按钮样式 */
.el-menu-item:first-child {
  border-bottom: 1px solid #444;
  margin-bottom: 5px;
  color: #ffd04b;
}

/* 菜单项激活状态 */
.el-menu-item.is-active {
  background-color: #444 !important;
  color: #ffd04b !important;
  border-radius: 4px;
}

/* 鼠标悬停状态 */
.el-menu-item:hover {
  background-color: #333 !important;
}
</style>
